/* eslint-disable no-unused-vars */
/* eslint-disable space-before-function-paren */
/* eslint-disable no-mixed-spaces-and-tabs */
/* eslint-disable no-trailing-spaces */
/* eslint-disable indent */
/* eslint-disable semi */
/* eslint-disable space-infix-ops */
import axios from 'axios'
import {Message} from 'element-ui'
// axios.defaults.withCredentials = true;

axios.interceptors.request.use(config =>{
  return config
}, error => {
  return Promise.reject(error)
})
axios.interceptors.response.use(data => {
  return data
}, error => {
  return Promise.reject(error)
})

const baseUrl = 'http://120.25.162.182:8081/profession';
// const baseUrl = 'http://artislong.frpgz1.idcfengye.com/profession/';

function checkResult(resultCode) {
	resultCode = parseInt(resultCode);
	return resultCode === 0;
}

function successFuc(response) {
	const {resultCode, errorMsg} = response.data;
	if (!checkResult(resultCode)) {
		Message.error(errorMsg);
		return Promise.reject(response);
	}
	return response;
}

function errorFunc(error) {
	Message.error('网络错误，请稍后再试~');
	return Promise.reject(error);
}

export default {
	baseUrl: baseUrl,
	checkResult: checkResult,
	myAjax(config, dataFunction, errorFunction) {
		return axios(config).then(successFuc, errorFunc).then(dataFunction, errorFunction);
	},
	simpleAjax(config) {
		config.url = baseUrl + config.url;

		if (config.data) {
			config.data = {
				param: config.data
			};
		}

		return axios(config).then(successFuc, errorFunc);
	},
	formatTime(time) {
		const fullZero = (num) => {
			return num < 10 ? "0" + num : num.toString();
		};
		const date = new Date(time);
		return date.getFullYear() + "-" + fullZero(date.getMonth() + 1) + "-" + fullZero(date.getDate());
	}
}
